﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;

using BeeZ.CommonObjects.Interfaces;


namespace BeeZ.Client
{

    namespace Hive
    {
        /// <summary>
        /// A general implementation of a Hive.
        /// A Hive is the object which gathers the bees and control them.
        /// </summary>
        public static class MainHive
        {
            private static IList<IBee> bees = new List<IBee>();

            public static void RegisterBee(IBee b)
            {
                bees.Add(b);
                ThreadPool.QueueUserWorkItem(new WaitCallback(b.StartFlying));
                //TimerCallback timerCallback = new TimerCallback(b.StartFlying);               
            }
            public static void UnRegisterBee(IBee b)
            {
                //b.StopFlying();
                b.Pause();
                bees.Remove(b);
            }

            public static IList<IBee> GetBees()
            {
                return bees; // TODO return copy of these elements or immutable or dont return
            }

            public static void KillAll()
            {
                GC.Collect();
            }

            public static void ResumeAll()
            {
                foreach (IBee b in bees)
                {
                    b.Resume();
                }
                Console.WriteLine("And the Hive is on its way...");
            }

            public static void ResumeAll(Type t)
            {
                foreach (IBee b in bees)
                {
                    if (ContainsInterface(b.GetType(), t))
                    {
                        b.Resume();
                    }
                }
            }

            public static void PauseAll()
            {
                foreach (IBee b in bees)
                {
                    b.Pause();
                }
                Console.WriteLine("The Hive Paused");
            }

            public static void PauseAll(Type t)
            {
                foreach (IBee b in bees)
                {                    
                    if (t.IsInstanceOfType(b)) //TODO
                    //if (ContainsInterface(b.GetType(), t))
                    {
                        b.Pause();
                    }
                }
            }

            /// <summary>
            /// stops all the bees in the hive except for a given Ibee.
            /// </summary>
            public static void PauseExcept(Type t)
            {
                foreach (IBee b in bees)
                {
                    if (!ContainsInterface(b.GetType(), t))
                    {
                        b.Pause();
                    }
                }
                Console.WriteLine("The Hive stopped partially");
            }

            public static bool ContainsInterface(Type A, Type Q)
            {
                Type[] interfaces = A.GetInterfaces();
                foreach (Type type in interfaces)
                {
                    if (type == Q)
                        return true;
                }

                return false;
            }

            //public static IBee GetBeeByType(
        }
    }
}